package com.brspace.qualitycontrol.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brspace.qualitycontrol.entity.HouseApproval;
import com.brspace.qualitycontrol.service.HouseApprovalService;
import com.brspace.qualitycontrol.utils.BaseController;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.JsonResult;
import com.brspace.qualitycontrol.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

/**
 * <p>
 * 建房审批 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-02
 */
@Api(description = "农房审批")
@CrossOrigin
@RestController
@RequestMapping("/admin/house-approval")
public class HouseApprovalController extends BaseController{
    @Resource
    private HouseApprovalService houseApprovalService;

    /**
     * 添加房屋审批信息
     * @param hose
     * @return
     */
    @ApiOperation("基地和建房审批表")
    @PostMapping("addHouse")
    public R addHouse(/*@ApiParam(value="基建审批对象", required = true)
                          @RequestBody*/ HouseApproval hose){
        houseApprovalService.addHose(hose);
        return R.ok().message("添加成功！");
    }

    /**
     * 基建和建房列表
     * @return
     */
    @ApiOperation("基建和建房列表")
    @GetMapping("list")
    public R list(){
        List<HouseApproval> list = houseApprovalService.list();
        return R.ok().data("list",list);
    }

    /**
     * 分页列表
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("分页列表")
    @PostMapping("list/{page}/{limit}")
    public R pageList(
            @ApiParam(value = "当前页码",required = true) @PathVariable Long page,
            @ApiParam(value = "每页多少数据", required = true) @PathVariable Long limit){
        Page<HouseApproval> pageParam = new Page<>(page, limit);
        IPage<HouseApproval> pageModel = houseApprovalService.selectPage(pageParam, null);
        List<HouseApproval> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 根据ID查询单个数据
     * @param id
     * @return
     */
    @ApiOperation("根据ID查询")
    @PostMapping("findById/{id}")
    public R findById(@ApiParam(value = "id",required = true) @PathVariable String id){
        return R.ok().message("成功！").data("house",houseApprovalService.getById(id));
    }

    /**
     * 获取农房申请总数
     * @return
     */
    @ApiOperation("获取农房申请总数")
    @GetMapping("houseNum")
    public R houseNum(){
     return houseApprovalService.houseNum() ;
    }
    /**
     *
     * @param houseApproval
     * @return
     */
    @PostMapping("save")
    public JsonResult<Void> save(HouseApproval houseApproval){
        if (EmptyUtil.isEmpty(houseApproval.getName())){
            throw new SecurityException("用户名为空");
        }
        return new JsonResult<>(OK);
    }

}

